<?php 
namespace App\Http\Controllers\MobileApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\pre_forum_forum;
use App\Models\Mysql\pre_forum_post;

class BilingualReadingController extends Controller
{
	use GlobalData;

	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected $curTime;

	protected $debug;

	const BILINGUAL_READING_CH = '经典双语阅读';

	const PAGINATE_SIZE = 10; //手机分页每页显示记录的个数.

	/**
	 * 创建新的`BilingualReadingController`控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		if(config('app.debug')) {
			$this->debug = true;
			error_reporting(E_ALL | E_STRICT);
		} else {
			$this->debug = false;
		}
	}

	/**
	 * API: 双语阅读主题列表接口(bilingualReadingThreadList)
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function ListBilingualReading()
	{
		try {
			if( !$this->check($this->request)) return responseJson($this->getError());
			if( !$this->request->boardChoice ) return responseJson('未获取到参数: 版块Id(boardChoice)');
			if( !$this->request->paegNo ) return responseJson('未获取到参数: 页号(paegNo)');
			$boardChoice = intval($this->request->boardChoice);
			$paegNo = intval($this->request->paegNo);

			if(FALSE != $r=$this->getBilingualReading($boardChoice, $paegNo)) {
				return responseJson('success', $r, TRUE);
			}
			return responseJson($this->getError());

		} catch (\Exception $e) {
			if($this->debug) {
				return responseJson(__FILE__.':'.__FUNCTION__.':'.__LINE__.':'.$e->getMessage());
			}
			return responseJson($e->getMessage());
		}
	}

	/**
	 * 服务于接口方法：ListBilingualReading
	 * @param $boardID    int 论团板块ID。
	 * @param int $paegNo 页号，从1开始。
	 * @return array|bool 返回结果。
	 */
	protected function getBilingualReading($boardID, $paegNo=1)
	{
		$forumID = pre_forum_forum::getFIDbyName(self::BILINGUAL_READING_CH);
		if( empty($forumID) ) return $this->setError('未获取到forum id: fid.');
		$forumID = intval($forumID[0]->fid);

		$boardList = pre_forum_forum::getBoardInfoByForumID($forumID);

		$curBoardInfo = [];
		$fid = $boardID;
		if(-1 == $boardID) {
			if( !empty($boardList) ) {
				foreach ($boardList as $item) {
					if($item->boardName == '经典赏析') {
						$fid = $item->boardId;
					}
				}
			}
		}

		if(-1 != $fid) {
			$curBoardInfo = pre_forum_post::getMessageOfBoardByForumID([
				'fid' => $fid,
				'pageNO' => $paegNo,
				'pageSize' => self::PAGINATE_SIZE
			]);
		}
		foreach($curBoardInfo as $k=>$v){
			$curBoardInfo[$k]->body = messagecutstr($v->body);
		}
		
		return [
			'boardList' => $boardList,
			'threadList' => $curBoardInfo,
			'threadListEmptyPicUrl' => self::getEmptyPicUrl('threadListEmptyPicUrl'),
			'hasNextPage' => empty($curBoardInfo) ? FALSE : TRUE
		];
	}

} //!- BilingualReadingController
?>
